﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Scap.Info;
using Scap.Data;

namespace Scap.Web.Controllers.Admin
{
    public class ScapDepartmentAdminController : Controller
    {
        private ScapData db = new ScapData();

        //
        // GET: /ScapDepartmentAdmin/

        public ActionResult Index()
        {
            var departments = db.Departments.Include(s => s.Type).Include(s => s.Address).Include(s => s.ParentDepartment).Include(s => s.Company);
            return View(departments.ToList());
        }

        //
        // GET: /ScapDepartmentAdmin/Details/5

        public ActionResult Details(int id = 0)
        {
            ScapDepartment scapdepartment = db.Departments.Find(id);
            if (scapdepartment == null)
            {
                return HttpNotFound();
            }
            return View(scapdepartment);
        }

        //
        // GET: /ScapDepartmentAdmin/Create

        public ActionResult Create()
        {
            ViewBag.TypeId = new SelectList(db.DepartmentTypes, "Id", "Name");
            ViewBag.AddressId = new SelectList(db.LocationAddresses, "Id", "Name");
            ViewBag.ParentDepartmentId = new SelectList(db.Departments, "Id", "Name");
            ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name");
            return View();
        }

        //
        // POST: /ScapDepartmentAdmin/Create

        [HttpPost]
        public ActionResult Create(ScapDepartment scapdepartment)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(scapdepartment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.TypeId = new SelectList(db.DepartmentTypes, "Id", "Name", scapdepartment.TypeId);
            ViewBag.AddressId = new SelectList(db.LocationAddresses, "Id", "Name", scapdepartment.AddressId);
            ViewBag.ParentDepartmentId = new SelectList(db.Departments, "Id", "Name", scapdepartment.ParentDepartmentId);
            ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name", scapdepartment.CompanyId);
            return View(scapdepartment);
        }

        //
        // GET: /ScapDepartmentAdmin/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ScapDepartment scapdepartment = db.Departments.Find(id);
            if (scapdepartment == null)
            {
                return HttpNotFound();
            }
            ViewBag.TypeId = new SelectList(db.DepartmentTypes, "Id", "Name", scapdepartment.TypeId);
            ViewBag.AddressId = new SelectList(db.LocationAddresses, "Id", "Name", scapdepartment.AddressId);
            ViewBag.ParentDepartmentId = new SelectList(db.Departments, "Id", "Name", scapdepartment.ParentDepartmentId);
            ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name", scapdepartment.CompanyId);
            return View(scapdepartment);
        }

        //
        // POST: /ScapDepartmentAdmin/Edit/5

        [HttpPost]
        public ActionResult Edit(ScapDepartment scapdepartment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(scapdepartment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeId = new SelectList(db.DepartmentTypes, "Id", "Name", scapdepartment.TypeId);
            ViewBag.AddressId = new SelectList(db.LocationAddresses, "Id", "Name", scapdepartment.AddressId);
            ViewBag.ParentDepartmentId = new SelectList(db.Departments, "Id", "Name", scapdepartment.ParentDepartmentId);
            ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name", scapdepartment.CompanyId);
            return View(scapdepartment);
        }

        //
        // GET: /ScapDepartmentAdmin/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ScapDepartment scapdepartment = db.Departments.Find(id);
            if (scapdepartment == null)
            {
                return HttpNotFound();
            }
            return View(scapdepartment);
        }

        //
        // POST: /ScapDepartmentAdmin/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ScapDepartment scapdepartment = db.Departments.Find(id);
            db.Departments.Remove(scapdepartment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}